package com.competition.commgr.service.impl;

import java.util.List;

import com.competition.commgr.domain.Competitionitem;
import com.competition.commgr.domain.Competitions;
import com.competition.commgr.service.ICompetitionitemService;
import com.competition.commgr.service.ICompetitionsService;
import com.competition.common.exception.ServiceException;
import com.competition.common.utils.StringUtils;
import com.competition.common.utils.bean.BeanValidators;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.competition.commgr.mapper.CompetitionsessionsMapper;
import com.competition.commgr.domain.Competitionsessions;
import com.competition.commgr.service.ICompetitionsessionsService;

import javax.validation.Validator;

/**
 * 赛项届次Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-01-29
 */
@Service
public class CompetitionsessionsServiceImpl implements ICompetitionsessionsService 
{
    @Autowired
    private CompetitionsessionsMapper competitionsessionsMapper;

    @Autowired
    private ICompetitionsService competitionsService;

    @Autowired
    protected Validator validator;
    @Autowired
    private ICompetitionitemService competitionsItemService;
    private static final Logger log = LoggerFactory.getLogger(CompetitionsessionsServiceImpl.class);

    /**
     * 查询赛项届次
     * 
     * @param sessionId 赛项届次主键
     * @return 赛项届次
     */
    @Override
    public Competitionsessions selectCompetitionsessionsBySessionId(Long sessionId)
    {
        return competitionsessionsMapper.selectCompetitionsessionsBySessionId(sessionId);
    }

    /**
     * 查询赛项届次列表
     * 
     * @param competitionsessions 赛项届次
     * @return 赛项届次
     */
    @Override
    public List<Competitionsessions> selectCompetitionsessionsList(Competitionsessions competitionsessions)
    {
        return competitionsessionsMapper.selectCompetitionsessionsList(competitionsessions);
    }

    /**
     * 新增赛项届次
     * 
     * @param competitionsessions 赛项届次
     * @return 结果
     */
    @Override
    public int insertCompetitionsessions(Competitionsessions competitionsessions)
    {
        return competitionsessionsMapper.insertCompetitionsessions(competitionsessions);
    }

    /**
     * 修改赛项届次
     * 
     * @param competitionsessions 赛项届次
     * @return 结果
     */
    @Override
    public int updateCompetitionsessions(Competitionsessions competitionsessions)
    {
        return competitionsessionsMapper.updateCompetitionsessions(competitionsessions);
    }

    /**
     * 批量删除赛项届次
     * 
     * @param sessionIds 需要删除的赛项届次主键
     * @return 结果
     */
    @Override
    public int deleteCompetitionsessionsBySessionIds(Long[] sessionIds)
    {
        return competitionsessionsMapper.deleteCompetitionsessionsBySessionIds(sessionIds);
    }

    /**
     * 删除赛项届次信息
     * 
     * @param sessionId 赛项届次主键
     * @return 结果
     */
    @Override
    public int deleteCompetitionsessionsBySessionId(Long sessionId)
    {
        return competitionsessionsMapper.deleteCompetitionsessionsBySessionId(sessionId);
    }

    @Override
    public String importCompetitionitem(List<Competitionsessions> list, boolean updateSupport, String operName) {
        if (StringUtils.isNull(list) || list.size() == 0)
        {
            throw new ServiceException("导入赛事类别数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        successMsg.append("导入成功");
        successMsg.append("【");
        StringBuilder failureMsg = new StringBuilder();
        for (Competitionsessions competitionsessions : list){
            try{
                // 验证是否存在这个赛项
                Competitionsessions  competitionsessions1= competitionsessionsMapper.selectOneCompetitionsessions(competitionsessions);
                if (competitionsessions1 == null){
                    BeanValidators.validateWithException(validator, competitionsessions);
                    //验证赛事名称是否正确
                    Competitions competitions = competitionsService.selectCompetitionsByCompetitionName(competitionsessions.getCompetitionName());
                    if (competitions==null){
                        failureNum++;
                        String msg = "<br/>" + failureNum +  " 导入失败：赛事名称【"+ competitionsessions.getCompetitionName() + "】错误";
                        failureMsg.append(msg);
                        continue;
                    }else {
                        competitionsessions.setCompetitionId(competitions.getCompetitionId());
                    }

                    //验证赛项名称是否正确
                    Competitionitem competitionitem = competitionsItemService.selectCompetitionitemByName(competitionsessions.getComItemName());

                    if (competitionitem==null){
                        failureNum++;
                        String msg = "<br/>" + failureNum +  " 导入失败：赛项名称【"+ competitionsessions.getComItemName() + "】错误";
                        failureMsg.append(msg);
                        continue;
                    }else {
                        competitionsessions.setComItemId(competitionitem.getComItemId());
                    }

                    competitionsessions.setCreateBy(operName);
                    competitionsessionsMapper.insertCompetitionsessions(competitionsessions);
                    successNum++;
                }
                else if (updateSupport)
                {
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum +  " 导入失败："
                            + competitionsessions.getComItemName() + "  " + competitionsessions.getYear() + "已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum +  " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if(failureNum == 0){
            successMsg.append(successNum);
            successMsg.append("】条数据");
            return successMsg.toString();
        }else {
            return failureMsg.toString();
        }

    }

    @Override
    public List<Competitionsessions> getSessionSuggestions(String keyword) {
        return competitionsessionsMapper.getSessionSuggestions(keyword);
    }
}
